<?
/*
		图片上传类
*/
class uploadfile {
	var $upfile_type,$upfile_size,$upfile_name,$upfile;
	var $d_alt,$extention_list,$tmp,$arri;
	var $datetime,$date;
	var $filestr,$size,$ext,$check;
	var $flash_directory,$extention,$file_path,$base_directory;
	var $url;
	
	# 实例化自己
	public static function & singleton()
	{
		static $instances;
		if (!isset ($instances))
		{
			$instances = new uploadfile();
		}
		return $instances;
	}	
	
	function uploadfile() {
		$this->set_url("index.php"); 
		$this->set_extention(); 
		$this->set_size(1024);//最大上传大小 
		$this->set_date(); 
		$this->set_datetime(''); 
		$this->set_base_directory("attachmentFile"); 
	}
	function set_file_type($upfile_type) {
		$this->upfile_type = $upfile_type; 
	}
	function set_file_name($upfile_name) {
		$this->upfile_name = $upfile_name; 
	}
	function set_upfile($upfile) {
		$this->upfile = $upfile;
	}
	function set_file_size($upfile_size) {
		$this->upfile_size = $upfile_size;
	}
	function set_url($url) {
		$this->url = $url; 
	}
	function get_extention() {
		$this->extention = preg_replace('/.*\.(.*[^\.].*)*/iU','\\1',$this->upfile_name); 
	}
	function set_datetime($id) {
		$this->datetime = date("YmdHis").'_'.$id; 
	}
	function set_date() {
		$this->date = date("Y-m-d"); 
	}
	function set_extention($extention_list='gif|jpg|jpeg|bmp|png') {
		$this->extention_list = $extention_list;
	}
	function set_size($size) {
		$this->size = $size;
	}
	function set_base_directory($directory) {
		$this->base_directory = $directory; 
	}
	function set_flash_directory() {
		$this->flash_directory = $this->base_directory."/".$this->date;
	}
	function showerror($errstr="未定义"){
		echo "<script language=javascript>alert('$errstr');window.location='javascript:history.go(-1);';</script>";
		exit();
	}
	function go_to($str,$url) {
		//$this->showerror($str);
		 echo "<script language='javascript'>alert('$str');window.location='$url';</script>"; 
		exit();
	}
	function mk_base_dir() {
		if (! file_exists($this->base_directory)){
			@mkdir($this->base_directory,0777);
		}
	}
	function mk_dir() {
		if (! file_exists($this->flash_directory)){
			@mkdir($this->flash_directory,0777);
		}
	}
	function get_compare_extention() {
		$this->ext = explode("|",$this->extention_list);
	}
	function check_extention() {
		for($i=0;each($this->ext);$i++)
		{
			if($this->ext[$i] == strtolower($this->extention)){
				$this->check = true; 
				break;
			}
		}
		if(!$this->check){
			$this->showerror("File type error!");
			//$this->showerror("格式错误，目前只支持以下格式".$this->extention_list."！");
		}
	}
	function check_size() {
		if($this->upfile_size > round($this->size*1024)){
			$this->showerror("File size error!");
			//$this->showerror("您上传的文件大小受限，请控制在 ".$this->size."KB 内");
		}
	}
	function set_file_path() {
		 $this->file_path = $this->flash_directory."/".$this->datetime."_".iconv("utf-8","gb2312",$this->upfile_name);//.".".$this->extention; 
//$this->file_path = $this->flash_directory."/".$this->datetime."_".$this->upfile_name;		
	}
    function get_file_path(){
    	return iconv("gb2312","utf-8",$this->file_path);
			//return $this->file_path;
    }
	function save() {
		$this->set_flash_directory();
		$this->get_extention();
		$this->get_compare_extention();
		$this->check_extention();
		$this->check_size(); 
		$this->mk_base_dir();
		$this->mk_dir(); 
		$this->set_file_path();
		if(copy($this->upfile,$this->file_path)){ 
			return true;
		}else {
			return false;
		}
	}
}
?>